<?php

/**
 * 后台系统配置参数模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: config.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class configAction extends Action
{

    private $tmp_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('config');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin');
    }

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {
        //TODO  取出所有资讯
        $rs = $this->tmp_model->getConfigList();
        $this->V('config');
    }

    /**
     * 新增/修改系统配置参数
     */
    public function add()
    {
        $sysid = !empty($_GET['sysid']) ? (int) $_GET['sysid'] : 0;
        if ($sysid > 0) {
            $editinfo = $this->tmp_model->getConfigInfo($sysid);
        } else {
            $editinfo['type'] = '';
            $editinfo['varname'] = '';
            $editinfo['value'] = '';
            $editinfo['info'] = '';
            $editinfo['help'] = '';
            $editinfo['order'] = '';
            $editinfo['nameaction'] = '';
            $editinfo['item'] = '';
            $editinfo['sys_id'] = '';
        }
        //取系统配置参数类型radiobutton数组
        $vartype_ary = UtilityConfig::vartype_ary();
        $vartype_ary_radio = Utility::RadioButton($vartype_ary, 'vartype', $editinfo['type'], 'onclick="typechange(this.value);"');

        $this->assign('vartype_ary_radio', $vartype_ary_radio);
        $this->assign('editinfo', $editinfo);

        $this->V('config');
    }

    /**
     * 新增/修改系统配置参数页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 4) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $sysid = !empty($_POST['sysid']) ? (int) $_POST['sysid'] : 0;
        $nvarname = !empty($_POST['nvarname']) ? trim($this->H($_POST['nvarname'])) : '';
        $nvarvalue = !empty($_POST['nvarvalue']) ? $_POST['nvarvalue'] : '';
        $vartype = !empty($_POST['vartype']) ? $_POST['vartype'] : '';
        $nameaction = !empty($_POST['nameaction']) ? trim($this->H($_POST['nameaction'])) : '';
        $item = !empty($_POST['item']) ? trim($this->H($_POST['item'])) : '';
        $varmsg = !empty($_POST['varmsg']) ? $_POST['varmsg'] : '';
        $help = !empty($_POST['help']) ? trim($this->H($_POST['help'])) : '';
        $order = !empty($_POST['order']) ? $_POST['order'] : '0';

        /* 验证不能为空 */
        if (empty($nvarname)) {
            $this->redirect('变量名称不能为空');
            exit;
        }

        if (!isset($vartype)) {
            $this->redirect('选择变量类型!');
            exit;
        }
        // TODO goon to verify

        $info = array(
            'varname' => $nvarname,
            'value' => $nvarvalue,
            'type' => $vartype,
            'nameaction' => $nameaction,
            'item' => $item,
            'info' => $varmsg,
            'help' => $help,
            'sys_order' => $order
        );
        if ($sysid > 0) {
            $config_page = HttpResponse::getCookie('config_page');
            //update save article
            $where = 'sys_id = ' . $sysid . '';
            $rs = $this->tmp_model->addConfig($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改系统配置参数成功', PHP_SELF . '?m=config&page= ' . $config_page . '');
            } else {
                $this->redirect('修改系统配置参数失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addConfig($info);
            if ($rs) {
                $this->redirect('添加系统配置参数成功', PHP_SELF . '?m=config.add');
            } else {
                $this->redirect('添加系统配置参数失败');
            }
        }
    }

    public function savelist()
    {
        if ($_POST['cfg_rewrite'] == 1) {
            /*
            $rewrite_search = $this->tmp_model->getConfigInfo(13);
            $rewrite_search = $rewrite_search['value'];
            $hotel_info_name = $this->tmp_model->getConfigInfo(14);
            $hotel_info_name = $hotel_info_name['value'];
                   
            $htaccess_search = array('^' . $rewrite_search, '^' . $hotel_info_name);
            $htaccess_replace = array('^' . $_POST['cfg_rewrite_search'], '^' . $_POST['cfg_hotel_info']);
            */
            $htaccess = $_POST['cfg_rewrite_rule'];     
            $htaccess_file = WEB_ROOT . '.htaccess';
//          $htaccess = str_replace($htaccess_search, $htaccess_replace, $htaccess);
            file_put_contents($htaccess_file, htmlspecialchars_decode(stripslashes($htaccess)), LOCK_EX);
            $_POST['cfg_rewrite_rule'] = $htaccess;
        }
        foreach ($_POST as $k => $v) {
            $value = $this->H($v);
            $info = array(
                'value' => $value
            );
            $where = "varname = '{$k}'";
            $rs = $this->tmp_model->addConfig($info, 'UPDATE', $where);
        }
        $configfile = APPLICATION_ROOT . 'Config' . DIRECTORY_SEPARATOR . 'configcache.inc.php';

        $this->tmp_model->ReWriteConfig($configfile);

        if ($rs) {
            $this->redirect('修改系统配置参数成功', PHP_SELF . '?m=config');
        } else {
            $this->redirect('修改系统配置参数失败');
        }
    }

}